/**
 * 
 */
package slhs.tempo.TableAnalyzer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableModel;

import slhs.tempo.scriptGenerator.UtilityConstants;

/**
 * ProbeFilterTextChangeListener.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class ProbeFilterTextChangeListener implements DocumentListener {
    private TablePanelHandler tablePanelHandlerObject;
    private FilterControlsPanelHandler filterControlsPanelHandlerObject;
    private static final String COMMA = ",";
    private static final String OR = "|";
    private List<String> originalProbesList;
    private boolean isCalledFirstTime = true;
    private int probeColumnIndex = -1;

    /**
     * @param tablePanelHandlerObject
     * @param filterControlsPanelHandler
     */
    public ProbeFilterTextChangeListener(TablePanelHandler tablePanelHandlerObject,
	    FilterControlsPanelHandler filterControlsPanelHandlerObject) {
	this.tablePanelHandlerObject = tablePanelHandlerObject;
	this.filterControlsPanelHandlerObject = filterControlsPanelHandlerObject;

    }

    /**
     * @return
     * 
     */
    private void populateProbesList() {
	/*
	 * Start with a list of probes separated b ORs Once user starts typing, remove any matches from the list and
	 * construct regular expression
	 */
	TableModel currentModel = ConcreteTableModelListener.getUpdatedModel();
	int columnCount = currentModel.getColumnCount();
	int rowCount = currentModel.getRowCount();
	this.originalProbesList = new ArrayList<String>(rowCount);
	if (isCalledFirstTime) {
	    for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
		if (currentModel.getColumnName(columnIndex).equals(UtilityConstants.PROBE_COLUMN_IDENTIFIER)) {
		    probeColumnIndex = columnIndex;
		}
	    }
	    if (probeColumnIndex == -1) {
		String index = JOptionPane.showInputDialog(
			"Cannot find a column named Probe. Please provide the column index. Index starts at 0",
			UtilityConstants.DEFAULT_PROE_COLUMN_INDEX);
		probeColumnIndex = Integer.valueOf(index);
	    }
	    isCalledFirstTime = false;
	}

	for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
	    originalProbesList.add((String) currentModel.getValueAt(rowIndex, probeColumnIndex));
	}

    }

    private String getSearchRegularExpression() {

	if (isCalledFirstTime) {
	    populateProbesList();
	    isCalledFirstTime = false;
	}

	List<String> regExpProbesList = new ArrayList<String>();
	for (String value : originalProbesList) {
	    regExpProbesList.add(value);
	}
	String filterText = filterControlsPanelHandlerObject.getIgnoreProbesText();
	String[] filterValues = filterText.split(COMMA);
	regExpProbesList.removeAll(Arrays.asList(filterValues));

	// now build a regular expression from this list
	StringBuilder regularExpressionBuilder = new StringBuilder();
	for (String currentString : regExpProbesList) {
	    regularExpressionBuilder.append(currentString + OR);
	}
	// construct regular expression with last OR removed
	String regExp = regularExpressionBuilder.substring(0, regularExpressionBuilder.length() - 1);
	String finalRegExp = regExp.replace("\\", "\\\\");
	return finalRegExp;

    }

    @Override
    public void changedUpdate(DocumentEvent event) {
	if (filterControlsPanelHandlerObject.hasProbeSearchActive())
	    tablePanelHandlerObject.plainTextFilter(getSearchRegularExpression());
    }

    @Override
    public void insertUpdate(DocumentEvent event) {

	if (filterControlsPanelHandlerObject.hasProbeSearchActive())
	    tablePanelHandlerObject.plainTextFilter(getSearchRegularExpression());
    }

    @Override
    public void removeUpdate(DocumentEvent event) {

	if (filterControlsPanelHandlerObject.hasProbeSearchActive())
	    tablePanelHandlerObject.plainTextFilter(getSearchRegularExpression());
    }
}
